/**
 * Created by csy on 2017/11/20.
 */
var $alert = require("libs/alert");
var isSale = require('libs/user-type').isSale;
require(['jquery','easyui'],function($){
    appendOption('api/v1/p/crm/warehouse/query?warehouseType=A',$("#warehouseId"),"warehouseId","warehouseName",{warehouseId:'',warehouseName:'全部'});
    $.parser.parse();
    tableInit();
    //查询按钮点击事件
    $("#main_search").on("click",function () {
        $('#main_table').datagrid('load',queryParams());
    });
    $(document).on("click",'a[name=pass]',function () {
        ajaxRequestMethod({id: $(this).data('id'),status:"P"},"api/v1/p/mms/ticket/checkContent");
    });
    $(document).on("click",'a[name=fail]',function () {
        ajaxRequestMethod({id: $(this).data('id'),status:"F"},"api/v1/p/mms/ticket/checkContent");
    });
    $(document).on("click",'a[name=look]',function () {
        window.parent.tabOpen($(this).attr('ref'),"工单详情",true,true);
    });
});

function tableInit() {
    $("#main_table").datagrid({
        url:'api/v1/p/mms/ticket/contentList',
        border : false,
        height:$(window).height(),
        pagination : true,
        idField : 'id',
        pageSize : 20,
        pageList : [ 20, 50,  100 ],
        rownumbers:true,
        singleSelect: true,
        emptyMsg : "暂无数据",
        queryParams:queryParams(),
        columns:[[ {
            field : 'ticketNo',
            title : '工单编号',
            formatter: function(value,row,index){
                return "<a name='look' href='javascript:;' ref='ticket-detail?ticketId="+row.ticketId+"'>"+value+"</a>";
            }
        },{
            field : 'content',
            title : '消息内容'
        },{
            field:'customerName',
            title:'关联客户'
        },{
            field:'warehouseName',
            title:'关联仓库'
        },{
            field:'createDt',
            title:'发送时间'
        },{
            field:'status',
            title:'状态',
            formatter:function (value,row,index) {
                switch (value){
                    case "W": return "待审核";
                    case "P": return "审核通过";
                    case "F": return "审核不通过";
                    default : return "";
                }
            }
        },{
            field:'opt',
            title:'操作',
            hidden:isSale,
            formatter:function (value,row,index) {
                switch (row.status){
                    case "W": return "<a name='look' href='javascript:;' ref='ticket-detail?ticketId="+row.ticketId+"'>查看消息</a>|<a href='javascript:;' name='pass' data-id='"+row.id+"'>通过</a>|<a href='javascript:;' name='fail' data-id='"+row.id+"'>不通过</a>";
                    case "P":
                    case "F": return "<a name='look' href='javascript:;' ref='ticket-detail?ticketId="+row.ticketId+"'>查看消息</a>";
                    default : return "";
                }
            }
        }
        ]],
        loadFilter:function(data){
            if(data.code=='200'){
                return data.data;
            }else{
                return [];
            }
        },
        toolbar:"#tb"
    });
}
function queryParams() {
    var d1 = $('#startDt').datebox('getValue');
    var d2 = $('#endDt').datebox('getValue');
    var data =
        {
            ticketNo: $('#ticketNo').val(),
            customerName: $('#customerName').val(),
            warehouseId: $('#warehouseId').combobox('getValue'),
            status: $('#status').combobox('getValue'),
            dateFrom: d1 ? $('#startDt').data('utc0') : '',
            dateTo: d2 ? $('#endDt').data('utc0') : ''
        };
    return data;
}
function ajaxRequestMethod(data,url) {
    $.ajax({
        contentType: "application/json; charset=utf-8",
        data:JSON.stringify(data),
        type:"post",
        url:url,
        success:function (result) {
            if(result.code==200){
                $('#main_table').datagrid('load');
            }else {
                $alert(result.message);
            }
        },
        error:function () {
            console.log(url+" error")
        }
    });
}
//添加参数
function appendOption(url,$select,valueField,textField,all) {
    $select.combobox({
        url:url,
        valueField:valueField,
        textField:textField,
        loadFilter:function(result){
            if(result.code=='200'){
                result.data.unshift(all);
                return result.data;
            }
        }
    })
}